/* This do file provides the code to generate Table 1 models G to L 
 in the main text. The 
procedure is described in detail in Appendix 7. */


global dataDir "D:\swweschl\Dropbox\Party_Interactions_Perceptions\PSRM Submission\Replication Files"
use "$dataDir/replication_data.dta", clear






set more off


/*create a dyad-survey ID */
capture drop dyad
egen dyad=group(groupdyad year)


/* create group based on habitual news reception where 1=low, 2=medium 3=high */

capture drop pk_group
gen pk_group=1 if sumpk<3 & (countryyear==1|countryyear==2)
replace pk_group=2 if (sumpk==3|sumpk==4) & (countryyear==1|countryyear==2)
replace pk_group=3 if sumpk>4  & (countryyear==1|countryyear==2)

replace pk_group=1 if sumpk<4 & countryyear==3
replace pk_group=2 if sumpk==4 & countryyear==3
replace pk_group=3 if sumpk>4 & countryyear==3

replace pk_group=1 if sumpk<2 & countryyear==4
replace pk_group=2 if (sumpk==2|sumpk==3) & countryyear==4
replace pk_group=3 if sumpk>3 & countryyear==4

replace pk_group=1 if sumpk<3 & countryyear==5
replace pk_group=2 if (sumpk==3|sumpk==4) & countryyear==5
replace pk_group=3 if sumpk>4 & countryyear==5

replace pk_group=1 if sumpk<3 & countryyear==6
replace pk_group=2 if (sumpk==3|sumpk==4) & countryyear==6
replace pk_group=3 if sumpk>4 & countryyear==6

replace pk_group=1 if sumpk<2 & countryyear==7
replace pk_group=2 if (sumpk==2|sumpk==3) & countryyear==7
replace pk_group=3 if sumpk>3 & countryyear==7
				 


/*Interactive model */

/* placeholder to store the 500 coefficients of various variables in different models */
foreach model in G H I J K L{

foreach group in MI HI{

capture drop mediascore_`group'_`model'
gen mediascore_`group'_`model'=.

capture drop mediascoreSE_`group'_`model'
gen mediascoreSE_`group'_`model'=.

capture drop pkgroup_`group'_`model'
gen pkgroup_`group'_`model'=.

capture drop pkgroupSE_`group'_`model'
gen pkgroupSE_`group'_`model'=.

}

capture drop mediascore_`model'
gen mediascore_`model'=.

capture drop mediascoreSE_`model'
gen mediascoreSE_`model'=.

capture drop ideological_distance_`model'
gen ideological_distance_`model'=.

capture drop ideological_distanceSE_`model'
gen ideological_distanceSE_`model'=.

capture drop cab_membership_`model'
gen cab_membership_`model'=.

capture drop cab_membershipSE_`model'
gen cab_membershipSE_`model'=.

capture drop constant_`model'
gen constant_`model'=.

capture drop constantSE_`model'
gen constantSE_`model'=.

capture drop dyadRE_`model'
gen dyadRE_`model'=.

capture drop dyadRE_SE_`model'
gen dyadRE_SE_`model'=.


capture drop respRE_`model'
gen respRE_`model'=.

capture drop respRE_SE_`model'
gen respRE_SE_`model'=.

capture drop residRE_`model'
gen residRE_`model'=.

capture drop residRE_SE_`model'
gen residRE_SE_`model'=.


}

capture drop partyA_temp
capture drop partyB_temp

egen partyA_temp=group(dyad_name1 country)
egen partyB_temp=group(dyad_name2 country)


/* Model G */ 

set more off 
local row=1
foreach row of numlist 1/500{


tempvar order
generate long `order' = _n


capture drop onedraw_partyA_rile
gen onedraw_partyA_rile=rnormal(rile_partyA1,rile_partyA_sd1) /* draw one rile score for party A from the distribution */
bysort partyA_temp: replace onedraw_partyA_rile = onedraw_partyA_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */



capture drop onedraw_partyB_rile
gen onedraw_partyB_rile=rnormal(rile_partyB1,rile_partyB_sd1) /* draw one rile score for party B from the distribution */
bysort partyB_temp: replace onedraw_partyB_rile = onedraw_partyB_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */
sort `order'


capture drop diff_rile /* calculate the diff in rile among the pair */
gen diff_rile=abs(onedraw_partyA_rile-onedraw_partyB_rile)

capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */


mixed coop c.onedraw_coopscore_full##i.pk_group /// /* use that one draw of the media score in this regression */
				 diff_rile ///
				 current_bothCab, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_G=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_G=_se[onedraw_coopscore_full] in `row' 				  
				  
replace mediascore_MI_G=_b[i2.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_MI_G=_se[i2.pk_group#c.onedraw_coopscore_full] in `row' 

replace mediascore_HI_G=_b[i3.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_HI_G=_se[i3.pk_group#c.onedraw_coopscore_full] in `row' 

replace pkgroup_MI_G=_b[i2.pk_group] in `row' 
replace pkgroupSE_MI_G=_se[i2.pk_group] in `row' 

replace pkgroup_HI_G=_b[i3.pk_group] in `row' 
replace pkgroupSE_HI_G=_se[i3.pk_group] in `row' 


replace ideological_distance_G=_b[diff_rile] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_G=_se[diff_rile] in `row' /* store the se of rile in this variable */

replace cab_membership_G=_b[current_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_G=_se[current_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_G=_b[_cons] in `row' 
replace constantSE_G=_se[_cons] in `row' 



replace dyadRE_G=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_G=r(se) in `row'

replace respRE_G=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_G=r(se) in `row'

replace residRE_G=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_G=r(se) in `row'


local row=`row'+1

}


/* Model H */ 

set more off 
local row=1
foreach row of numlist 1/500{

tempvar order
generate long `order' = _n

capture drop onedraw_partyA_rile
gen onedraw_partyA_rile=rnormal(rile_partyA1,rile_partyA_sd1) /* draw one rile score for party A from the distribution */
bysort rile_partyA1: replace onedraw_partyA_rile = onedraw_partyA_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */

capture drop onedraw_partyB_rile
gen onedraw_partyB_rile=rnormal(rile_partyB1,rile_partyB_sd1) /* draw one rile score for party B from the distribution */
bysort rile_partyB1: replace onedraw_partyB_rile = onedraw_partyB_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */
sort `order'

capture drop diff_rile /* calculate the diff in rile among the pair */
gen diff_rile=abs(onedraw_partyA_rile-onedraw_partyB_rile)


capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */



mixed coop c.onedraw_coopscore_full##i.pk_group /// /* use that one draw of the media score in this regression */
				 diff_rile ///
				 perceived_bothCab, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_H=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_H=_se[onedraw_coopscore_full] in `row' 				  
				  
replace mediascore_MI_H=_b[i2.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_MI_H=_se[i2.pk_group#c.onedraw_coopscore_full] in `row' 

replace mediascore_HI_H=_b[i3.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_HI_H=_se[i3.pk_group#c.onedraw_coopscore_full] in `row' 

replace pkgroup_MI_H=_b[i2.pk_group] in `row' 
replace pkgroupSE_MI_H=_se[i2.pk_group] in `row' 

replace pkgroup_HI_H=_b[i3.pk_group] in `row' 
replace pkgroupSE_HI_H=_se[i3.pk_group] in `row' 


replace ideological_distance_H=_b[diff_rile] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_H=_se[diff_rile] in `row' /* store the se of rile in this variable */

replace cab_membership_H=_b[perceived_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_H=_se[perceived_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_H=_b[_cons] in `row' 
replace constantSE_H=_se[_cons] in `row' 

replace dyadRE_H=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_H=r(se) in `row'

replace respRE_H=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_H=r(se) in `row'

replace residRE_H=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_H=r(se) in `row'


local row=`row'+1

}


/* Model I */ 

set more off 
local row=1
foreach row of numlist 1/500{

tempvar order
generate long `order' = _n


capture drop onedraw_partyA_rile
gen onedraw_partyA_rile=rnormal(rile_partyA1,rile_partyA_sd1) /* draw one rile score for party A from the distribution */
bysort rile_partyA1: replace onedraw_partyA_rile = onedraw_partyA_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */

capture drop onedraw_partyB_rile
gen onedraw_partyB_rile=rnormal(rile_partyB1,rile_partyB_sd1) /* draw one rile score for party B from the distribution */
bysort rile_partyB1: replace onedraw_partyB_rile = onedraw_partyB_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */
sort `order'


capture drop diff_rile /* calculate the diff in rile among the pair */
gen diff_rile=abs(onedraw_partyA_rile-onedraw_partyB_rile)


capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */



mixed coop c.onedraw_coopscore_full##i.pk_group /// /* use that one draw of the media score in this regression */
				 diff_rile ///
				 percent_month120_bothCab, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_I=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_I=_se[onedraw_coopscore_full] in `row' 				  
				  
replace mediascore_MI_I=_b[i2.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_MI_I=_se[i2.pk_group#c.onedraw_coopscore_full] in `row' 

replace mediascore_HI_I=_b[i3.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_HI_I=_se[i3.pk_group#c.onedraw_coopscore_full] in `row' 

replace pkgroup_MI_I=_b[i2.pk_group] in `row' 
replace pkgroupSE_MI_I=_se[i2.pk_group] in `row' 

replace pkgroup_HI_I=_b[i3.pk_group] in `row' 
replace pkgroupSE_HI_I=_se[i3.pk_group] in `row' 


replace ideological_distance_I=_b[diff_rile] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_I=_se[diff_rile] in `row' /* store the se of rile in this variable */

replace cab_membership_I=_b[percent_month120_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_I=_se[percent_month120_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_I=_b[_cons] in `row' 
replace constantSE_I=_se[_cons] in `row' 

replace dyadRE_I=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_I=r(se) in `row'

replace respRE_I=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_I=r(se) in `row'

replace residRE_I=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_I=r(se) in `row'



local row=`row'+1

}


/* Model J */ 

set more off 
local row=1
foreach row of numlist 1/500{

capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */



mixed coop c.onedraw_coopscore_full##i.pk_group /// /* use that one draw of the media score in this regression */
				 lrdist ///
				 current_bothCab, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_J=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_J=_se[onedraw_coopscore_full] in `row' 				  
				  
replace mediascore_MI_J=_b[i2.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_MI_J=_se[i2.pk_group#c.onedraw_coopscore_full] in `row' 

replace mediascore_HI_J=_b[i3.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_HI_J=_se[i3.pk_group#c.onedraw_coopscore_full] in `row' 

replace pkgroup_MI_J=_b[i2.pk_group] in `row' 
replace pkgroupSE_MI_J=_se[i2.pk_group] in `row' 

replace pkgroup_HI_J=_b[i3.pk_group] in `row' 
replace pkgroupSE_HI_J=_se[i3.pk_group] in `row' 


replace ideological_distance_J=_b[lrdist] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_J=_se[lrdist] in `row' /* store the se of rile in this variable */

replace cab_membership_J=_b[current_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_J=_se[current_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_J=_b[_cons] in `row' 
replace constantSE_J=_se[_cons] in `row' 

replace dyadRE_J=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_J=r(se) in `row'

replace respRE_J=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_J=r(se) in `row'

replace residRE_J=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_J=r(se) in `row'

local row=`row'+1

}



/* Model K */ 

set more off 
local row=1
foreach row of numlist 1/500{

capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */




mixed coop c.onedraw_coopscore_full##i.pk_group /// /* use that one draw of the media score in this regression */
				 lrdist ///
				 perceived_bothCab, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_K=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_K=_se[onedraw_coopscore_full] in `row' 				  
				  
replace mediascore_MI_K=_b[i2.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_MI_K=_se[i2.pk_group#c.onedraw_coopscore_full] in `row' 

replace mediascore_HI_K=_b[i3.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_HI_K=_se[i3.pk_group#c.onedraw_coopscore_full] in `row' 

replace pkgroup_MI_K=_b[i2.pk_group] in `row' 
replace pkgroupSE_MI_K=_se[i2.pk_group] in `row' 

replace pkgroup_HI_K=_b[i3.pk_group] in `row' 
replace pkgroupSE_HI_K=_se[i3.pk_group] in `row' 


replace ideological_distance_K=_b[lrdist] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_K=_se[lrdist] in `row' /* store the se of rile in this variable */

replace cab_membership_K=_b[perceived_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_K=_se[perceived_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_K=_b[_cons] in `row' 
replace constantSE_K=_se[_cons] in `row' 

replace dyadRE_K=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_K=r(se) in `row'

replace respRE_K=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_K=r(se) in `row'

replace residRE_K=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_K=r(se) in `row'

local row=`row'+1

}


/* Model L */ 

set more off 
local row=1
foreach row of numlist 1/500{

capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */




mixed coop c.onedraw_coopscore_full##i.pk_group /// /* use that one draw of the media score in this regression */
				 lrdist ///
				 percent_month120_bothCab, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_L=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_L=_se[onedraw_coopscore_full] in `row' 				  
				  
replace mediascore_MI_L=_b[i2.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_MI_L=_se[i2.pk_group#c.onedraw_coopscore_full] in `row' 

replace mediascore_HI_L=_b[i3.pk_group#c.onedraw_coopscore_full] in `row' 
replace mediascoreSE_HI_L=_se[i3.pk_group#c.onedraw_coopscore_full] in `row' 

replace pkgroup_MI_L=_b[i2.pk_group] in `row' 
replace pkgroupSE_MI_L=_se[i2.pk_group] in `row' 

replace pkgroup_HI_L=_b[i3.pk_group] in `row' 
replace pkgroupSE_HI_L=_se[i3.pk_group] in `row' 


replace ideological_distance_L=_b[lrdist] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_L=_se[lrdist] in `row' /* store the se of rile in this variable */

replace cab_membership_L=_b[percent_month120_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_L=_se[percent_month120_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_L=_b[_cons] in `row' 
replace constantSE_L=_se[_cons] in `row' 

replace dyadRE_L=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_L=r(se) in `row'

replace respRE_L=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_L=r(se) in `row'

replace residRE_L=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_L=r(se) in `row'


local row=`row'+1


}


keep mediascore_MI_G- residRE_SE_L




set more off
foreach model in G H I J K L {

rename dyadRE_SE_`model' dyadRESE_`model'
rename respRE_SE_`model' respRESE_`model'
rename residRE_SE_`model' residRESE_`model'

rename mediascoreSE_MI_`model' mediascore_MISE_`model'
rename mediascoreSE_HI_`model' mediascore_HISE_`model'

rename pkgroupSE_MI_`model' pkgroup_MISE_`model'
rename pkgroupSE_HI_`model' pkgroup_HISE_`model'


set more off
foreach i of numlist 1/500{


foreach variable in mediascore mediascore_MI mediascore_HI ///
pkgroup_MI pkgroup_HI ideological_distance cab_membership ///
 constant dyadRE respRE residRE{
 
capture drop `variable'`model'_simcoef

gen `variable'`model'_simcoef=rnormal(`variable'_`model',`variable'SE_`model')

}

preserve
keep *_simcoef
tempfile tempscore`model'_`i'
	save `tempscore`model'_`i''
	
restore 
}
}

foreach model in G H I J K L {
use `tempscore`model'_1', clear

foreach i of numlist 2/500{

append using `tempscore`model'_`i''

}
}


/* The coefficients of the variables in all the models are the average of 
the simulated coefficients while
the bootstrapped standard errors are the standard deviation */ 


sum *G_simcoef /*Model G */
sum *H_simcoef /*Model H */
sum *I_simcoef /*Model I */
sum *J_simcoef /*Model J */
sum *K_simcoef /*Model K */
sum *L_simcoef /*Model L */




